import { areaList } from '../../utils/cityData';
import { addrressDetail,addAddrress,editeAddrress, } from '../../../../../common/api/index'
Page({
    data: {
		receiveMobile: null,//手机号
		receiveName: null,//联系人
		receiveAddress: null,//门牌号
		areaList: areaList,//省市区地址
		showChooese: false,//选择省市区地址
		receiveProvince: null,//省
		receiveCity: null,//市
		receiveArea: null,//区
		defaultFlag: false,//是否默认
		proviceId: null,
		cityId: null,
		areaId: null,
		addrressId: null,//修改id
	},
    onLoad(options) {
		// 如果有id，说明是地址修改
		if(options.id){
			this.setData({
				addrressId: options.id
			})
			this.getAddrressDetail()
		}
	},
	// 保存地址
	submitSave(){
		if(!this.data.receiveAddress){
			wx.showToast({
				title: '请正确输入门牌号',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.receiveName){
			wx.showToast({
				title: '请正确输入联系人',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.receiveMobile){
			wx.showToast({
				title: '请正确输入手机号',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.receiveProvince || !this.data.receiveCity || !this.data.receiveArea){
			wx.showToast({
				title: '请正确选择省市区',
				icon: 'none',
				duration: 2000
			})
			return
		}
		let data = {
			receiveName: this.data.receiveName,//收货人名称
			receiveMobile: this.data.receiveMobile,//电话
			receiveProvince: this.data.receiveProvince,
			receiveCity: this.data.receiveCity,
			receiveArea: this.data.receiveArea,
			receiveAddress: this.data.receiveAddress,//收货人详细地址
			defaultFlag: this.data.defaultFlag?1:0,//是否默认(0否，1是)
		}
		if(this.data.addrressId){// 存在 则是修改地址
			data.receiveId = this.data.addrressId
			editeAddrress(data).then(res => {
				if(res.code == 200){
					wx.showToast({
						title: '修改地址成功',
						icon: 'none',
						duration: 2000
					})
					setTimeout(()=>{
						wx.navigateBack()
					},2000)
				}
			})
		}else{// 不存在则是新增地址
			addAddrress(data).then(res => {
				if(res.code == 200){
					wx.showToast({
						title: '新增地址成功',
						icon: 'none',
						duration: 2000
					})
					setTimeout(()=>{
						wx.navigateBack()
					},2000)
				}
			})
		}

	},
	// 加载地址详情
	getAddrressDetail(){
		addrressDetail(this.data.addrressId).then(res => {
			if(res.code == 200){
				this.setData({
					receiveMobile: res.data.receiveMobile,//手机号
					receiveName:  res.data.receiveName,//联系人
					receiveAddress:  res.data.receiveAddress,//门牌号
					receiveProvince:  res.data.receiveProvince,//省
					receiveCity:  res.data.receiveCity,//市
					receiveArea:  res.data.receiveArea,//区
					defaultFlag: res.data.defaultFlag,//是否默认
				})
			}
		})
	},
	// 是否勾选默认
	onChangeDefault(){
		this.setData({
			defaultFlag: !this.data.defaultFlag
		})
	},
	// 地区选择
	comfirmChooese(e){
		this.setData({
			receiveProvince: e.detail.values[0].name,
			receiveCity: e.detail.values[1].name,
			receiveArea: e.detail.values[2].name,
			proviceId: e.detail.values[0].code,
			cityId: e.detail.values[1].code,
			areaId: e.detail.values[2].code,
			showChooese: false
		})
	},
	// 点击选择省市区
	clickChooese(){
		this.setData({
			showChooese: true
		})
	},
	// 输入变化
	onChange(e){
		let key = e.target.dataset.name;
		this.setData({
			[key]: e.detail
		})
	},


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})